package com.caijin.web.controller.system;


import com.caijin.common.core.domain.Result;
import com.caijin.common.core.domain.entity.Catalogue;
import com.caijin.system.service.ICatalogueService;
 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程目录表 前端控制器
 * </p>
 *
 * @author Peas
 * @since 2021-01-31
 */
@RestController
@RequestMapping("/system/catalogue")
public class CatalogueController {

    @Autowired
    private ICatalogueService catalogueService;

    /**
     * 获取目录列表
     */
    @RequestMapping(path = "/getList", method = RequestMethod.GET)
    public Result getList(@RequestParam("cno") Long cno) {
        return catalogueService.getChapterList(cno);
    }

    /**
     * 获取某个目录内容
     */
    @RequestMapping(path = "/get", method = RequestMethod.GET)
    public Result getCatalogue(@RequestParam("id") Long id){
        return catalogueService.getChapterById(id);
    }

    /**
     * 删除章节
     */
    @RequestMapping(path = "/delete", method = RequestMethod.DELETE)
    public Result deleteChapter(@RequestParam("chapterId") Long id,
                                @RequestParam("cno") Long cno) {
        return catalogueService.deleteChapter(id, cno);
    }

    /**
     * 插入章节
     */
    @RequestMapping(path = "/insert", method = RequestMethod.POST)
    public Result insert(@RequestBody Catalogue catalogue) {
        int insert = catalogueService.getBaseMapper().insert(catalogue);
        return insert != 0 ? Result.ok().message("添加章节成功") : Result.error().message("添加章节失败");
    }

    /**
     * 更新章节
     */
    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Catalogue data) {
        return catalogueService.updateById(data) ? Result.ok() : Result.error();
    }
}

